function Game() {
	
	this.board_temp = {};
	this.board = {};
	this.level = 1;
	this.board_width = 10;
	this.board_height = 10;
	this.sx = 0;
	this.sy = 0;
	this.path = new Array();
	this.score = 0;
	
	this.start = function() {
		this.level = 1;
		this.board = {};
		this.generate_level();
		this.score = 0;
	}
	
	this.next_level = function() {
		this.level++;
		this.generate_level();
	}
	
	this.get_block = function(x,y){
		return this.board[x+y*this.board_width];
	}
	
	this.generate_level = function() {
		for(var i=0;i<100;i++) {
			this.board[i] = 1 + Math.floor(Math.random()*(this.level+1));
		}
	}
	
	this.block_click = function(x,y) {
		var found = this.get_similar(x,y);
		this.mark_board();
		return found;
	}
	
	this.reset_temp = function() {
		for(var i=0;i<100;i++) {
			this.board_temp[i] = -1;
		}
	}
	
	this.mark_board = function() {
		for(var i=0;i<100;i++) {
			if(this.board_temp[i]==1) {
				this.board[i] = 0;
			}
		}
	}

	this.fix_board = function() {
		var bonus = 0;
		for(var i=0;i<100;i++) {
			if(this.board[i]==0) {
				this.board[i] = -1;
				bonus++;
			}
		}
		this.score += bonus*bonus;
		for(var x=0; x<this.board_width; x++) {
			for(var y=0; y<this.board_height; y++) {
				if(this.board[x+y*this.board_width]==-1) {
					for(var my=y; my>0; my--) {
						this.board[x+my*this.board_width] = this.board[x+(my-1)*this.board_width];
					}
					this.board[x] = -1;
				}
			}
		}
		var vertical = 0;
		var movement = 0;
		do {
			for(var x=0; x<this.board_width; x++) {
				vertical=0;
				for(var y=0; y<this.board_height; y++)
				{
					if(this.board[x+y*this.board_width]!=-1) {
						vertical = 1;
					}
				}

				if(vertical==0) {
					movement++;
					for(var vx=x; vx<(this.board_width-1); vx++) {
						for(var y=0; y<this.board_height; y++) {
							this.board[vx+y*this.board_width] = this.board[vx+1+y*this.board_width];
						}
					}
					for(y=0; y<this.board_height; y++) {
						this.board[this.board_width-1+y*this.board_width] = -1;
					}
				}
			}
		}while(movement<120 && vertical==0);
		return this.score;
	}

	
	this.get_direction = function(px,py,target_color) {
		// Up
		if(py>0 && this.board_temp[px + (py-1)*this.board_width]==-1 && this.board[px + (py-1)*this.board_width]==target_color)
		{
			sx=0;
			sy=-1;
			return true;
		}
		// Right
		if(px<(this.board_width-1) && this.board_temp[(px+1)+py*this.board_width]==-1 && this.board[px+1+py*this.board_width]==target_color)
		{
			sx=1;
			sy=0;
			return true;
		}
		// Down
		if(py<(this.board_height-1) && this.board_temp[px+(py+1)*this.board_width]==-1 && this.board[px+(py+1)*this.board_width]==target_color)
		{
			sx=0;
			sy=1;
			return true;
		}
		// Left
		if(px>0 && this.board_temp[(px-1)+py*this.board_width]==-1 && this.board[(px-1)+py*this.board_width]==target_color)
		{
			sx=-1;
			sy=0;
			return true;
		}
		sx=0;
		sy=0;
		return false;		
	}
	
	this.get_similar = function(x,y) {
		this.reset_temp();		
		var target_color = this.get_block(x,y);
		if(target_color<=0) {
			return 0;
		}
		var found = 0;
		this.path = new Array();
		this.path[0] = new Object();
		this.path[0].x = x;
		this.path[0].y = y;
		var xx = 0;
		var yy = 0;
		var pos = 0; 
		do{
			xx = this.path[pos].x;
			yy = this.path[pos].y;
			if(this.get_direction(xx,yy,target_color)==true) {
				pos++;
				this.path[pos] = new Object();
				this.path[pos].x = xx + sx;
				this.path[pos].y = yy + sy;
				this.board_temp[(xx+sx) + (yy+sy)*this.board_width] = 1;
				found++;
			} else {
				if(pos==0) break; // We are ready
				pos--;
			}
		}while(true);
		return found;
	}
	
}